package app.Disthanitian.thaistyle.EmerMez;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.ActionBar.OnNavigationListener;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import app.Disthanitian.thaistyle.EmerMez.AddContactView1.RunDeleteContactList;
import app.Disthanitian.thaistyle.EmerMez.AddContactView1.RunDeleteIDCONTACT;


public class SettingActivity extends PreferenceActivity {
	Intent newAct;
	UpdateTimeLocate update = new UpdateTimeLocate();
	UpdateMsgYellow updateYel = new UpdateMsgYellow();
	UpdateMsgRed updateRed = new UpdateMsgRed();
	private static final int RESUEST_CODE = 1;
	
	MemberUser user = new MemberUser();
	String msgYellow;
	String msgRed;
	int TimeValue= user.getTimeLocateUpdate();
	//------------OnCreate
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.setting);
        actionBar();
        
		Preference profile = getPreferenceManager().findPreference("editprofile");
		Preference password = getPreferenceManager().findPreference("editpassword");
		Preference pincode = getPreferenceManager().findPreference("editpincode");
		Preference contact = getPreferenceManager().findPreference("contact");
		Preference timetracking = getPreferenceManager().findPreference("timetracking");
		Preference MSGYel = getPreferenceManager().findPreference("msgYelllow");
		Preference MSGRed = getPreferenceManager().findPreference("msgRed");
		Preference myaccount = getPreferenceManager().findPreference("myaccount");
	
		
		profile.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				if (preference.getKey().equals("editprofile")) {
					startActivityA();
					return true;
				}
				
				return false;
			}
		});
		
		
		 password.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				if (preference.getKey().equals("editpassword")) {
					startActivityB();
					return true;
				}
				
				return false;
			}
		});
		
		
		 contact.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				if (preference.getKey().equals("contact")) {
					startActivityC();
					return true;
				}
				
				return false;
			}
		});
		 
		 pincode.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				if (preference.getKey().equals("editpincode")) {
					startActivityD();
					return true;
				}
				
				return false;
			}
		});
		
		 timetracking.setOnPreferenceClickListener(new OnPreferenceClickListener() {

				@Override
				public boolean onPreferenceClick(Preference preference) {
					if (preference.getKey().equals("timetracking")) {
						
						DialogSetTimeTrack();
						
						return true;
					}
					
					return false;
				}

			});
		 MSGYel.setOnPreferenceClickListener(new OnPreferenceClickListener() {

				@Override
				public boolean onPreferenceClick(Preference preference) {
					if (preference.getKey().equals("msgYelllow")) {
						
						DialogSetMessageYellow();
						
						return true;
					}
					
					return false;
				}

			});
		 MSGRed.setOnPreferenceClickListener(new OnPreferenceClickListener() {

				@Override
				public boolean onPreferenceClick(Preference preference) {
					if (preference.getKey().equals("msgRed")) {
						 DialogSetMessageRed();
						
						
						return true;
					}
					
					return false;
				}

			});

	}
	
 	public void actionBar(){
		
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle("Settings");
	
		
	}
	
	private void DialogSetTimeTrack(){
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View layout = inflater.inflate(R.layout.timetraking, (ViewGroup) findViewById(R.id.LayoutSeekBar));
	    

	    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog)).setView(layout);
	    builder.setTitle("Time Traking (Minute)");
	    
	    final TextView item1 = (TextView) layout.findViewById(R.id.TimeValue);
	    
		 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int id) {
	            	SetTime();	  	            	
	            }
	        })
	      .setNegativeButton("No", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int id) {
	                 dialog.cancel();
	            }
	        });
	     // ---------------------
		 
	    SeekBar sb = (SeekBar)layout.findViewById(R.id.seekBarTime);
	    
		sb.setProgress(TimeValue);
		item1.setText("TimeUpdate : " + TimeValue);
		
	    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
	        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
	            //Do something here with new value
	        	
	        	TimeValue = progress;
	        	item1.setText("TimeUpdate : " + TimeValue);
	        	
	        }

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
			   //	item1.setText("Value of : " + TimeValue);
				//seekBar.setProgress(TimeValue);
				
			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				 user.setTimeLocateUpdate(TimeValue);
				
			}
	    });
        
	    
	    AlertDialog alertDialog = builder.create();
	    alertDialog.show();
	
		
	}
	
    public void SetTime(){
       update.callRun();
	    	
	    }
    
    public void SetYellow(){
    	updateYel.callRun();
 	    	
 	    }
    
    public void SetRed(){
        updateRed.callRun();
	    
	    }
    
	private void DialogSetMessageYellow(){
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View layout = inflater.inflate(R.layout.messageyellow, (ViewGroup) findViewById(R.id.MSGYELLOW));
	
	    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog)).setView(layout);
	    builder.setTitle("Yellow Status Message");
	   
	    final EditText item2 = ( EditText) layout.findViewById(R.id.MessageYellow);
	    item2.setText(user.getMsgYellow());
	    
		 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int id) {
	            	
	            	String value =  item2.getText().toString();	            	
	            	user.setMsgYellow(value);
	            	SetYellow();	  	            	
	            }
	        })
	      .setNegativeButton("No", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int id) {
	                 dialog.cancel();
	            }
	        });
	     // ---------------------
		 
        AlertDialog alertDialog = builder.create();
	    alertDialog.show();
	  
	}
	
	private void DialogSetMessageRed(){
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View layout = inflater.inflate(R.layout.messagered, (ViewGroup) findViewById(R.id.MSGRED));
	    
	    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.myDialog)).setView(layout);
	    builder.setTitle("Red Status Message");
	
	    final EditText item2 = ( EditText) layout.findViewById(R.id.MessageRed);
	    item2.setText(user.getMsgRed());
	    
		 builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int id) {
	            	String value =  item2.getText().toString();	            	
	            	user.setMsgRed(value);
	            	SetRed();	  	            	
	            }
	        })
	      .setNegativeButton("No", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int id) {
	                 dialog.cancel();
	            }
	        });
	     // ---------------------
		 
        AlertDialog alertDialog = builder.create();
	    alertDialog.show();
	    
		
	}
	

	private void startActivityA() {
		newAct = new Intent(this, EditMyAccount.class);
		startActivityForResult(newAct, 1);
	}
	private void startActivityB() {
		newAct = new Intent(this, ChangePassword.class);
		startActivityForResult(newAct, 1);
	}
	private void startActivityC() {
		newAct = new Intent(this, AddContactView1.class);
		startActivityForResult(newAct, 1);
	}
	
	private void startActivityD() {
		newAct = new Intent(this, PinCodeActivity.class);
		startActivityForResult(newAct, 1);
		
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 1 && resultCode == RESULT_OK) {
			// do something here
		}
	}
	
	
	
	
	
	public static String getMessageRed(Context context){
		MemberUser u = new MemberUser();
		//return PreferenceManager.getDefaultSharedPreferences(context).getString("msgRed", ""+u.getFirstname()+" may be in risk because "+u.getFirstname()+" doesn't interact with the phone.");
		
		return PreferenceManager.getDefaultSharedPreferences(context).getString("msgRed","Test");
	}
	public static String getMessageYellow(Context context){
		MemberUser u = new MemberUser();
		//return PreferenceManager.getDefaultSharedPreferences(context).getString("msgRed", ""+u.getFirstname()+" may be in risk because "+u.getFirstname()+" doesn't interact with the phone.");
		
		return PreferenceManager.getDefaultSharedPreferences(context).getString("msgYel","Test");

	}
	
	public static String getPinCode(Context context){
		MemberUser u = new MemberUser();
		//return PreferenceManager.getDefaultSharedPreferences(context).getString("msgRed", ""+u.getFirstname()+" may be in risk because "+u.getFirstname()+" doesn't interact with the phone.");
		
		return PreferenceManager.getDefaultSharedPreferences(context).getString("pinCode","1234");
	}
   
}
